#!/bin/bash
#author: xieziqiang
#email: xie.zi.qiang@outlook.com

# https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.14.3-linux-x86_64.tar.gz
# https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.22-linux-x86_64.tar.gz

set -e
#****** vars *********#
FILEBEAT_VERSION="8.14.3"
FILEBEAT_INSTALL_PATH="/usr/local"
FILEBEAT_DATA_PATH="/data/filebeat-data"
FILEBEAT_LOG_PATH="/data/filebeat-logs"

#****** functions *********#
color() {
    local message="$1"
    local status="$2"
    local terminal_width
    terminal_width=$(tput cols)
    local middle_column=$(( terminal_width / 2))
    local SETCOLOR_SUCCESS="\033[1;32m"  # 绿色
    local SETCOLOR_FAILURE="\033[1;31m"  # 红色
    local SETCOLOR_WARNING="\033[1;33m"  # 黄色
    local SETCOLOR_NORMAL="\033[0m"      # 重置颜色
    printf '%s\n\033[%sG' "${message}"  "${middle_column}"
    # Print the status message
    printf "["
    case ${status} in
        success|0)
            echo -en  "${SETCOLOR_SUCCESS}  OK  "
            ;;
        failure|1)
            echo -en  "${SETCOLOR_FAILURE}FAILED"
            ;;
        *)
            echo -en "${SETCOLOR_WARNING}WARNING"
            ;;
    esac
    echo -e "${SETCOLOR_NORMAL}]"
}

judge_current_user () {
    if [ "$(id -u)" = "0" ];then
        color "current user is administrator" 0
    else
        color "current user is not administrator" 1
        exit
    fi
}

create_dir () {
    local i
    for i in "$@";do
        if [ ! -d "$i" ];then
            mkdir -pv "$i" && color "$i dir is created" 0
        else
            color "$i dir is existed." 2
        fi
    done
}

process_file () {
    if [ ! -e "filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz" ];then
        color "current directory dont exist filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz" 1
        exit
    fi

    tar -xvf "filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz" -C "${FILEBEAT_INSTALL_PATH}"

    ln -nfs ${FILEBEAT_INSTALL_PATH}/filebeat-${FILEBEAT_VERSION}-linux-x86_64 ${FILEBEAT_INSTALL_PATH}/filebeat

}

init_filebeat_service () {
    cat > /lib/systemd/system/filebeat.service<<EOF
[Unit]
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
Documentation=https://www.elastic.co/beats/filebeat
Wants=network-online.target
After=network-online.target

[Service]
Environment="GODEBUG='madvdontneed=1'"
Environment="BEAT_LOG_OPTS="
Environment="BEAT_CONFIG_OPTS=-c ${FILEBEAT_INSTALL_PATH}/filebeat/filebeat.yml"
Environment="BEAT_PATH_OPTS=--path.home ${FILEBEAT_INSTALL_PATH}/filebeat --path.config ${FILEBEAT_INSTALL_PATH}/filebeat --path.data ${FILEBEAT_DATA_PATH} --path.logs ${FILEBEAT_LOG_PATH}"
ExecStart=${FILEBEAT_INSTALL_PATH}/filebeat/filebeat --environment systemd \$BEAT_LOG_OPTS \$BEAT_CONFIG_OPTS \$BEAT_PATH_OPTS
Restart=always

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable filebeat.service
    systemctl start filebeat.service \
    && color "filebeat start sucess!" 0 \
    || { color "filebeat start fail." 1; exit; } 
}

main () {
    judge_current_user
    process_file
    create_dir ${FILEBEAT_DATA_PATH} ${FILEBEAT_LOG_PATH}
    init_filebeat_service
}

main